﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Microsoft.Kinect;

namespace DetectingKinectSencor
{
    /// <summary>
    /// The following class was provided by the book 
    /// Microsoft.Press.Programming.with.the.Kinect.for.Windows.Software.Development.Kit.Sep.2012
    /// </summary>
    class RecordDepthStream
    {
        private bool DEBUGon = MainWindow.DEBUGrecordDepthStream;

        DateTime referenceTime;
        readonly BinaryWriter writer;

        /// <summary>
        ///Constuctor of RecordDepthStream
        /// </summary>
        internal RecordDepthStream(BinaryWriter writer)
        {
            this.writer = writer;
            referenceTime = DateTime.Now;
        }

        /// <summary>
        /// Method thad records the depth stream
        /// </summary>
        public void Record(DepthImageFrame frame)
        {

            // Write the hdeader, this is importat to distinguish between the three different streams
            // (at the end one file which includes the three streams)
            writer.Write((int)DetectingKinectSencor.RecordStreams.KinectRecordOptions.Depth);

            // Writing the data
            // Timespan is important, because a reference time is needed. Each recorder must keep a reference time
            // variable and add it to the saved stream to allow the player to reproduce the data at the same frequency
            TimeSpan timeSpan = DateTime.Now.Subtract(referenceTime);

            if (DEBUGon)
                Console.WriteLine("depth: Record: timeSpan: " + timeSpan.TotalMilliseconds);

            referenceTime = DateTime.Now;

            writer.Write((long)timeSpan.TotalMilliseconds);
            
            //Write number of bytes per pixel
            writer.Write(frame.BytesPerPixel);

            //Write the frame format
            writer.Write((int)frame.Format);

            //Write the with and height of the frame
            writer.Write(frame.Width);
            writer.Write(frame.Height);

            //Write the frame number
            writer.Write(frame.FrameNumber);

            if (DEBUGon)
                Console.WriteLine("depth: Record: FrameNumber: " + frame.FrameNumber);

            // Create a new object in which the pixels of the frame are copied to
            short[] depthShorts = new short[frame.PixelDataLength];

            // Copy the frame pixels into the short array
            frame.CopyPixelDataTo(depthShorts);

            // write the length of the short array, length stays the same
            // even when converting in bytes, because short (2bytes)
            // -> 1 byte + 1 byte
            writer.Write(depthShorts.Length);



            byte[] depthBytes = new byte[frame.PixelDataLength*2];
            byte[] depthBytesTemp = new byte[2];

            for(int i=0, j=0 ; j < depthShorts.Length ; i = i + 2 , j++)
            {
                depthBytesTemp = BitConverter.GetBytes(depthShorts[j]);
                depthBytes[i] = depthBytesTemp[0];
                depthBytes[i + 1] = depthBytesTemp[1];
            }

            writer.Write(depthBytes);
        }
    }
}
